package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.Document;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.DocumentService;
import com.kuiandjie.service.EmployeeService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.util.DateUtil;

public class EditDocumentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");
		String did = request.getParameter("did");
		if(m == null || "".equals(m) || did == null || "".equals(did)){
			response.sendRedirect("document.jspx");
			return;
		}
		HttpSession session = request.getSession();
		Employee e = (Employee) session.getAttribute("employee");
		DocumentService ds = new DocumentService();
		Document d = ds.findDocumentByIdAndDid(e.getId(),did);
		if("edit".equals(m)){
			if(d == null){
				response.sendError(403,"没有此权限进行修改！");
			}else{
			edit(request,response,did);
			}
		}else if("delete".equals(m)){
			if(d == null){
				response.sendError(403,"没有此权限进行删除！");
			}else{
				delete(request,response,did);
			}
			
		}else if("detail".equals(m)){
			detail(request,response,did);
		}else{
			response.sendRedirect("document.jspx");
		}
	}

	private void detail(HttpServletRequest request, HttpServletResponse response, String did) throws IOException, ServletException {
		
			DocumentService ds = new DocumentService();
			Document d = new Document();
			d = ds.findDocumentById(did);
			request.setAttribute("d", d);
			request.getRequestDispatcher("WEB-INF/views/docudetail.jsp").forward(request, response);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response, String did) throws IOException {
			DocumentService ds = new DocumentService();
			ds.deleteDocument(did);
			response.sendRedirect("document.jspx");
	}

	private void edit(HttpServletRequest request, HttpServletResponse response, String did) throws IOException, ServletException {
		
			DocumentService ds = new DocumentService();
			Document d = new Document();
			if("POST".equals(request.getMethod())){
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				String title = request.getParameter("title");
				String content = request.getParameter("content");
				
				ds.editDocument(title,content,did);
				Employee cure = (Employee) request.getSession().getAttribute("employee");
				Project curp = (Project) request.getSession().getAttribute("curp");
				Log log = new Log();
				log.setDesc("分享了资料<a href='editdocument.jspx?m=detail&did=" + did + "'>" + title + "</a>");
				log.setEmployeeId(cure.getId());
				log.setProjectId(curp.getId());
				log.setTime(DateUtil.getNow());
				log.setName(cure.getName());
				log.setPic(cure.getPic());
				
				LogService ls = new LogService();
				ls.inserLog(log);
				
				request.setAttribute("msg", new Gson().toJson(log));
				request.getRequestDispatcher("msgflow.jspx").include(request, response);
				out.print("1");
				out.flush();
				out.close();
			}else{
				d = ds.findDocumentById(did);
				request.setAttribute("d", d);
				request.setAttribute("did", did);
				request.getRequestDispatcher("WEB-INF/views/editdocument.jsp").forward(request, response);
			}
			
		
		
	}

}
